layui.define(['table', 'form', 'upload', 'admin'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , upload = layui.upload
        , admin = layui.admin;

    //查看照片
    admin.events.photosPreview = function (othis) {
        let urls = othis.attr("value");
        let urlarr = new Array();
        $.each(urls.split(","), function (i, val) {
            urlarr.push({"src": val});
        });
        layer.photos({
            photos: {
                "title": "查看照片" //相册标题
                , "data": urlarr
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };

    //申请入会列表
    table.render({
        elem: '#LAY-apply-user-manage'
        , url: config.apply_get_list
        , cols: [[
            {field: 'id', width: 80, title: 'ID', sort: true, fixed: 'left'}
            , {field: 'type_text', width: 100, title: '类型'}
            , {
                field: '', title: '主播ID', width: 150, templet: function (d) {
                    return d.user ? d.user.id : '';
                }
            }
            , {
                field: '', title: '昵称', minWidth: 150, templet: function (d) {
                    return d.user ? decodeURI(d.user.nick_name) : '';
                }
            }
            , {
                field: '', title: '头像', width: 80, templet: function (d) {
                    if (d.user.avatar) {
                        return "<img style='display: inline-block;  height: 100%;' src= " + d.user.avatar + " layadmin-event='photosPreview' value='" + d.user.avatar + "'>";
                    }
                    return '';
                }
            }
            , {
                field: '', width: 100, title: '性别', templet: function (d) {
                    if (!d.user.profile) {
                        return '未知';
                    }
                    switch (d.user.profile.gender) {
                        case 1:
                            return '男';
                        case 2:
                            return '女';
                        default:
                            return "未知";
                    }
                }
            }
            , {field: 'create_time', title: '申请时间', width: 190}
            , {
                field: '', title: '状态', width: 100, templet: function (d) {
                    switch (d.status) {
                        case 0:
                            return '待处理';
                        case 1:
                            return '审核通过';
                        case 2:
                            return '拒绝';
                        default:
                            return "未知";
                    }
                }
            }
            , {field: 'operate_time', title: '处理时间', width: 190}
            , {title: '操作', width: 170, align: 'center', fixed: 'right', toolbar: '#table-transfer-user-operation'}
        ]]
        , page: true
        , limit: 20
        , height: 'full-180'
        , text: '对不起，加载出现异常！'
        , even: true
    });


    //监听工具条
    table.on('tool(LAY-apply-user-manage)', function (obj) {

        if (obj.event === 'agree') {
            layer.confirm('确定同意该用户的申请吗？', function (index) {
                admin.req({
                    url: config.apply_operate_apply_status
                    , data: {"id": obj.data.id, 'status': 1}
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-apply-user-manage");
                            layer.msg(res.msg, {icon: 1});
                        }
                    }
                });
            });
        } else if (obj.event === 'refuse') {
            layer.confirm('确定拒绝该用户的申请吗？', function (index) {
                admin.req({
                    url: config.apply_operate_apply_status
                    , data: {"id": obj.data.id, 'status': 2}
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-apply-user-manage");
                            layer.msg(res.msg, {icon: 1});
                        }
                    }
                });
            });
        }
    });


    //转会申请列表
    table.render({
        elem: '#LAY-transfer-user-manage'
        , url: config.apply_get_transfer_list
        , cols: [[
            {field: 'id', width: 80, title: 'ID', sort: true, fixed: 'left'}
            , {
                field: '', width: 100, title: '类型', templet: function (d) {
                    switch (d.type) {
                        case 1:
                            return '转出';
                        case 2:
                            return '转入';
                        default:
                            return "未知";
                    }
                }
            }
            , {
                field: '', title: '主播ID', width: 100, templet: function (d) {
                    return d.user ? d.user.id : '';
                }
            }
            , {
                field: '', title: '昵称', minWidth: 150, templet: function (d) {
                    return d.user ? decodeURI(d.user.nick_name) : '';
                }
            }
            , {
                field: '', title: '头像', width: 80, templet: function (d) {
                    if (d.user.avatar) {
                        return "<img style='display: inline-block;  height: 100%;' src= " + d.user.avatar + " layadmin-event='photosPreview' value='" + d.user.avatar + "'>";
                    }
                    return '';
                }
            }
            , {
                field: '', width: 100, title: '性别', templet: function (d) {
                    if (!d.user.profile) {
                        return '未知';
                    }
                    switch (d.user.profile.gender) {
                        case 1:
                            return '男';
                        case 2:
                            return '女';
                        default:
                            return "未知";
                    }
                }
            }
            , {field: 'guild_audit_str', title: '转出代理审核', width: 130}
            , {field: 'into_guild_audit_str', title: '转入代理审核', width: 150}
            , {field: 'platform_audit_str', title: '平台审核状态', width: 130}
            , {field: 'create_time', title: '申请时间', width: 190}
            , {
                field: '', title: '状态', width: 100, templet: function (d) {
                    switch (d.status) {
                        case 0:
                            if (d.step == 1) {
                                return '待处理';
                            }
                            return '审核中';
                        case 1:
                            return '审核通过';
                        case 2:
                            return '拒绝';
                        default:
                            return "未知";
                    }
                }
            }
            , {field: 'operate_time', title: '处理时间', width: 190}
            , {title: '操作', width: 170, align: 'center', fixed: 'right', toolbar: '#table-transfer-user-operation'}
        ]]
        , page: true
        , limit: 20
        , height: 'full-180'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    //监听工具条
    table.on('tool(LAY-transfer-user-manage)', function (obj) {
        if (obj.event === 'agree') {
            layer.confirm('确定审核通过吗？', function (index) {
                admin.req({
                    url: config.apply_operate_transfer_status
                    , data: {"id": obj.data.id, 'status': 1, 'type': obj.data.type}
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-transfer-user-manage");
                            layer.msg(res.msg, {icon: 1});
                        }
                    }
                });
            });
        } else if (obj.event === 'refuse') {
            layer.confirm('确定拒绝申请吗？', function (index) {
                admin.req({
                    url: config.apply_operate_transfer_status
                    , data: {"id": obj.data.id, 'status': 2, 'type': obj.data.type}
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-transfer-user-manage");
                            layer.msg(res.msg, {icon: 1});
                        }
                    }
                });
            });
        }
    });


    exports('guild/apply', {})
});